//顶部有tab切换的列表（物流订单列表）
var basePage = require('../../../common/BasePage');
var baseList = require('../../../common/BaseList');
Page({
    behaviors: [basePage, baseList],

    /**
     * 页面的初始数据
     */
    data: {
        tabIdx: 0,
        tabArr: [{
                title: '未支付',
                type: 0,
                empty: "您暂无未支付订单"
            },
            {
                title: '已支付',
                type: 1,
                empty: "您暂无已支付订单"
            }
        ],
        total_price: 0,
        /**是否全选 */
        selectedAll: false,
        /**是否可以提交订单，防止多次提交*/
        canSubmit: true,
        // pay: "allin",
        // payType: 1,
        showPay:false,
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.reqUrl = options.reqUrl;
        this.data.reqData.type = options.type;
        if (options.client == "express") { //员工端
            // this.data.client=options.client;
            this.setData({ //默认选中已支付
                tabIdx: 1,
                client: options.client
            })
        }
        this.req_items();
        this.getPayType(1)
    },
    tab_change(e) {
        // console.log("tab_change",e);
        this.setData({
            tabIdx: e.detail.index,
            [`reqData.so`]: ''
        })
        this.data.reqData.type = this.data.tabArr[e.detail.index].type;
        this.data.current_page = 0
        this.req_items();
    },

    search_click(e) {
        // console.log("search_click", e);
        this.data.reqData.so = e.detail;
        this.data.reqData.type = this.data.tabArr[this.data.tabIdx].type;
        this.data.current_page = 0;
        this.req_items();
    },
    /**跳转员工物流订单支付页面 */
    pay_click() {
        wx.navigateTo({
            url: '/pages/cls/staff/list/Index',
        })
    },
    btn_click() {
        wx.navigateTo({
            url: '/pages/cls/qiugou_choosegoods/choose?type=1',
            events: {
                choose_pdc: function (data) {
                    console.log("选中的用户数据", data);
                    setTimeout(() => {
                        wx.navigateTo({
                            url: '/pages/mina/expressDetail/expressDetail?uid=' + data.uid + "&name=" + data.name,
                        })
                    }, 500);
                }
            }
        })
        // let express_oids = [];
        // let that = this;
        // this.data.itemArr.map(val => {
        //   express_oids.push(val.express_oid);
        // });
        // if (!express_oids.length) {
        //   return wx.showToast({
        //     title: '暂无可支付订单',
        //     icon: 'none',
        //   })
        // }
        // this.request({
        //   url: this.data.host + "/home/user/express/preinfo",
        //   data: {
        //     express_oids
        //   },
        // }, res => {
        //   console.log("预支付信息", res);
        //   wx.showModal({
        //     cancelText: '取消',

        //     confirmText: '立即支付',
        //     content: '您需要支付' + res.data.total_price + "元",
        //     showCancel: true,
        //     title: '提示',
        //     success: (result) => {
        //       if (result.confirm) {
        //         that.add_order(express_oids);
        //       }
        //     },
        //   })
        // });
    },
    /**添加支付订单 */
    add_order(express_oids) {
        if (!express_oids.length) {
            return wx.showToast({
                title: '请先选择要支付的订单',
                icon: 'none',
            })
        }
        this.request({
            url: this.data.host + "/home/user/express/handle",
            method: "post",
            data: {
                express_oids: JSON.stringify(express_oids)
            },
        }, res => {
            this.req_pay(res.data)
        });
    },
    init_val(val, index, arr) {
        val.created_at_label = val.created_at_label.split(" ")[0];
        // console.log( "val.created_at_label", val.created_at_label)
        if (val.lock_status) {
            val.pay_label = "已锁定";
        }
    },
    /**请求支付参数 */
    req_pay(data) {
        let that = this;
        this.request({
            url: this.data.host + '/home/payment/order',
            method: "POST",
            data: {
                order_id: data.order_id,
                code: this.data.pay
            }
        }, res => {
            if ( this.data.pay == "allin") {
                //通联支付
                var payInfo = res.data.payInfo
                if (payInfo) {
                    payInfo = JSON.parse(payInfo)
                    wx.requestPayment({
                        ...payInfo,
                        success(res) {
                            that.setData({
                                tabIdx: 1
                            })
                        },
                        fail(err) {
                            wx.showToast({
                                title: '支付失败',
                                icon: 'none'
                            })
                            console.error('pay fail', err)
                        },
                    })
                }else if(res.data.extraData){//跳转收银台小程序
                    var extraData=res.data.extraData

                    this.alinePay(extraData,this.pay_ok)

                }else{
                    wx.showToast({
                      title: '支付失败',
                      icon:"none"
                    })
                }
                
            } else if ( this.data.pay == "wxpay") {
                wx.requestPayment({
                    ...res.data.pay_data,
                    success(res) {
                        that.setData({
                            tabIdx: 1
                        })

                    },
                    fail(err) {
                        wx.showToast({
                            title: '支付失败',
                            icon: 'none'
                        })
                    }
                })
            }else{
                this.setData({
                    tabIdx: 1
                })
            }

        });
    },
    pay_ok(){
        this.setData({
            tabIdx: 1
        })
    },
    look_click(e) {
        wx.navigateTo({
            url: '/pages/cls/staff/Detail?reqUrl=/home/user/express/detail&express_oid=' + e.currentTarget.dataset.express_oid,
        })
    },
    /**选中元素 */
    select_item(e) {
        let index = e.currentTarget.dataset.index;
        // console.log("select_item", e);
        this.data.itemArr[index].checked = !this.data.itemArr[index].checked;
        if (this.data.tabIdx == 0) {
            this.update_total();
        }
    },
    /**更新总价 */
    update_total() {
        let total_price = 0;
        let itemArr = this.data.itemArr;
        let selectedAll = true;
        itemArr.map(val => {
            if (val.checked) {
                total_price += val.total_price * 1;
            } else {
                selectedAll = false;
            }

        });
        this.setData({
            total_price,
            itemArr,
            selectedAll
        })
    },
    all_change() {
        this.data.itemArr.map(val => {
            val.checked = !this.data.selectedAll;
        });
        this.update_total()
    },
    /**
     * 提交订单
     */
    submit_order() {
        if (!this.data.canSubmit) {
            return wx.showToast({
                title: '请勿频繁提交',
                icon: 'none'
            })
        }
        this.setData({
            showPay:false
        })
        this.data.canSubmit = false;
        setTimeout(() => {
            this.data.canSubmit = true;
        }, 1000);
        let express_oids = [];

        this.data.itemArr.map(val => {
            if (val.checked) {
                express_oids.push(val.express_oid);
            }
        });

        this.add_order(express_oids);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },




})